from django.db import models
import django.utils.timezone as timezone


# Create your models here.
class UserInfo(models.Model):
    username = models.CharField(verbose_name='用户名', max_length=32, db_index=True)
    email = models.CharField(verbose_name='邮箱', max_length=32)
    mobile_phone = models.CharField(verbose_name='手机号', max_length=32, unique=True)

    def __str__(self):
        return self.mobile_phone

    password = models.CharField(verbose_name='密码', max_length=32)

    gender_choices = (
        (0, "未设置"),
        (1, "男"),
        (2, "女"),
    )
    gender = models.CharField(verbose_name='性别', max_length=8, choices=gender_choices, default='未设置')
    address = models.CharField(verbose_name='地址', null=True, max_length=128)
    detail = models.CharField(verbose_name='详情', null=True, max_length=256)
    imgsrc = models.CharField(verbose_name='图片地址', null=True, max_length=128)

    # class Meta:
    #     unique_together = ('mobile_phone',)


class DeviceData(models.Model):
    send_choices = (
        ('1', '订阅'),
        ('2', '发布'),
    )
    mobile_phone = models.CharField(verbose_name='手机号', max_length=32)
    devtopic = models.ForeignKey(verbose_name='devtopic', to="DevInfo", on_delete=models.CASCADE)
    devdata = models.CharField(verbose_name='数据', null=True, max_length=32)
    datadetail = models.CharField(verbose_name='数据详情', null=True, max_length=32)
    send_flag = models.CharField(verbose_name='订阅/发布', max_length=20, choices=send_choices, default='1')
    datarecord = models.DateTimeField('保存时间', auto_now=True)
    triggerset = models.CharField(verbose_name='触发事件', null=True, max_length=32)
    characteristic = models.CharField(verbose_name='标识', null=True, max_length=32)



class DevInfo(models.Model):
    SUBSCRIBED = '订阅中'
    UNSUBSCRIBED = '未订阅'
    SUB_CHOICES = (
        (SUBSCRIBED, '订阅中'),
        (UNSUBSCRIBED, '未订阅')
    )
    send_choices = (
        ('1', '订阅'),
        ('2', '发布'),
    )
    devtype_choices = (
        ('1', '数据'),
        ('2', '视频'),
    )
    mobile_phone = models.ForeignKey("UserInfo", to_field="mobile_phone", on_delete=models.CASCADE)
    devname = models.CharField(verbose_name='设备名', max_length=32)
    send_flag = models.CharField(verbose_name='订阅/发布', max_length=20, choices=send_choices, default='1')
    devtopic = models.CharField(verbose_name='Topic', max_length=32)

    def __str__(self):
        return self.devtopic

    devsym = models.CharField(verbose_name='符号', max_length=32, blank=True)
    devtype = models.CharField(verbose_name='设备类型', choices=devtype_choices, max_length=16, default='1')
    detail = models.CharField(verbose_name='详情', null=True, blank=True, max_length=256)
    flag = models.CharField(verbose_name='状态', max_length=20, choices=SUB_CHOICES, default=UNSUBSCRIBED)
    devrecord = models.DateTimeField('保存时间', auto_now=True)
    srcpath = models.CharField(verbose_name='帧保存路径', null=True, max_length=128)


class ModelInfo(models.Model):
    mobile_phone = models.ForeignKey("UserInfo", to_field="mobile_phone", on_delete=models.CASCADE)
    modelname = models.CharField(verbose_name='模块名', max_length=32)
    detail = models.CharField(verbose_name='详情', null=True, blank=True, max_length=256)
    devrecord = models.DateTimeField('保存时间', auto_now=True)
    modelsrcpath = models.CharField(verbose_name='模块图片路径', null=True, max_length=128)


class ModelPart(models.Model):
    ModelID = models.ForeignKey(verbose_name='id', to="ModelInfo", on_delete=models.CASCADE, default='')
    reltopic_id = models.CharField(verbose_name='devtopic', max_length=32, default='')


class Scheduletask(models.Model):
    flag_choices = (
        ('1', '有效'),
        ('2', '失效'),
    )
    taskname = models.CharField(verbose_name='任务名称', max_length=32)
    tasktopic = models.ForeignKey(verbose_name='devtopic', to="DevInfo", on_delete=models.CASCADE)
    taskcont = models.CharField(verbose_name='发布任务', max_length=32)
    taskweek = models.CharField(verbose_name='任务周', max_length=32, null=True)
    taskhour = models.CharField(verbose_name='任务时', max_length=32, null=True)
    taskminute = models.CharField(verbose_name='任务分', max_length=32, null=True)
    tasktype = models.CharField(verbose_name='定时任务类型', max_length=32)
    taskuptodate = models.CharField(verbose_name='截止日期', max_length=32, null=True)
    flag = models.CharField(verbose_name='时效性', max_length=32, choices=flag_choices, default='1')
    tasksavetime = models.DateTimeField('任务保存时间', auto_now=True)
    mobile_phone = models.CharField(verbose_name='手机号', max_length=32, default='')


class Trigger(models.Model):
    triggername = models.CharField(verbose_name='事件名称', max_length=32, null=True)
    triggercalling = models.CharField(verbose_name='来源主题', max_length=32, null=True)
    triggercalled = models.CharField(verbose_name='目的主题', max_length=32, null=True)
    triggerdata = models.CharField(verbose_name='被检测的触发数据', max_length=32, null=True)
    pubdata = models.CharField(verbose_name='发布数据', max_length=32,null=True)
    triggersavetime = models.CharField(verbose_name='触发日期', max_length=32)
    mobile_phone = models.CharField(verbose_name='手机号', max_length=32, default='')
